package com.endoc.phtotapplication.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.endoc.phtotapplication.MainActivity;
import com.endoc.phtotapplication.R;
import com.endoc.phtotapplication.utils.SharePrefrencesUtils;
import com.endoc.phtotapplication.utils.StatusBarUtil;
import com.endoc.phtotapplication.utils.StringUtils;
import androidx.core.view.ViewCompat;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class DeviceImfSetActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
String TAG = "DeviceImfSetActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_imf_set);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
            //StatusBarUtil.setStatusBarColor(this, Color.parseColor("#FFFFFF"));
        }
        initData();
        initView();
    }
String [] requestPermissions =new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
    int ALL_REQUEST=1128;
    @Override
    public void initData() {
        if(!EasyPermissions.hasPermissions(mContext,requestPermissions)){
            EasyPermissions.requestPermissions(DeviceImfSetActivity.this,
                "你拒绝了权限,现在需要重新申请",//使用官方提供的弹窗(`AppSettingsDialog`)的时候会用到这个字符串,作为用户拒绝之后弹窗的内容
                ALL_REQUEST,
                requestPermissions
            );
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将结果交由EasyPermissions解决
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);

    }

    private EditText etDeviceId;
    private EditText etZlTb;
    private EditText etSqId;
    private EditText etJlDz;
    private EditText etTbDz;

    @Override
    public void initView() {
        etDeviceId = (EditText) findViewById(R.id.et_device_id);
        etZlTb = (EditText) findViewById(R.id.et_zl_tb);
        etSqId = (EditText) findViewById(R.id.et_sq_id);
        etJlDz = (EditText) findViewById(R.id.et_jl_dz);
        etTbDz = (EditText) findViewById(R.id.et_tb_dz);
        findViewById(R.id.tv_setting).setOnClickListener(this);

      /*  public static String deviceID="DEVICEID";
        public static String   zltb = "ZLTB";//增量同步
        public static String   qltb ="QLTB";//全量同步
        public static String recordAddress ="RECORDADDRESS";//记录地址
        public static String tbAddress = "TBADDRESS";//同步地址*/
        String deviceID = (String)SharePrefrencesUtils.getParam(mContext, StringUtils.deviceID, "");
        long zltb = (Long)SharePrefrencesUtils.getParam(mContext, StringUtils.zltb, (long)15);
        long qltb = (Long) SharePrefrencesUtils.getParam(mContext, StringUtils.qltb, (long)21600);
        String recordAddress = (String)SharePrefrencesUtils.getParam(mContext, StringUtils.recordAddress, "");
        String tbAddress = (String)SharePrefrencesUtils.getParam(mContext, StringUtils.tbAddress, "");
        //如果都有数据,并且不是从主界面来的,那么就直接跳转主界面
        if(!deviceID.isEmpty()&&!recordAddress.isEmpty()&&!tbAddress.isEmpty()&&!getIntent().getBooleanExtra(StringUtils.deviceImfor,false)){
            startActivity(new Intent(mContext,MainActivity.class));
            finish();
        }
        etDeviceId.setText(deviceID);
        etZlTb.setText(zltb+"");
        etSqId.setText(qltb+"");
        etJlDz.setText(recordAddress);
        etTbDz.setText(tbAddress);
    }
       void ToastShow(String content){
        Toast.makeText(mContext,content,Toast.LENGTH_SHORT).show();
       }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_setting:
                if(etDeviceId.getText().toString().trim().isEmpty()){
                    ToastShow("设备编号未填写");
                    return;
                }if(etZlTb.getText().toString().trim().isEmpty()){
                    ToastShow("增量同步未填写");
                    return;
                }if(etSqId.getText().toString().trim().isEmpty()){
                ToastShow("全量同步未填写");
                return;
                }if( etJlDz.getText().toString().trim().isEmpty()){
                ToastShow("记录地址未填写");
                return;
                }if(etTbDz.getText().toString().trim().isEmpty()){
                ToastShow("同步地址未填写");
                return;
                }

                SharePrefrencesUtils.setParam(mContext, StringUtils.deviceID, etDeviceId.getText().toString().trim());
                SharePrefrencesUtils.setParam(mContext, StringUtils.zltb, Long.parseLong(etZlTb.getText().toString().trim()));
                SharePrefrencesUtils.setParam(mContext, StringUtils.qltb, Long.parseLong(etSqId.getText().toString().trim()));
                SharePrefrencesUtils.setParam(mContext, StringUtils.recordAddress, etJlDz.getText().toString().trim());
                SharePrefrencesUtils.setParam(mContext, StringUtils.tbAddress, etTbDz.getText().toString().trim());

                    startActivity(new Intent(mContext,MainActivity.class));
                    finish();
                break;
            default:
                break;
        }

    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Log.d(TAG,"requestCode=="+requestCode+"     size=="+perms.size());
            if(requestCode==ALL_REQUEST){
                Toast.makeText(mContext,"你有权限未授权,程序退出",Toast.LENGTH_LONG).show();
                finish();
            }
    }


}
